<?php
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/GaleryItem.interface.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/HumeurDatabase.class.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/classes/Uploader.class.php");
		
	class Mood implements GaleryItem{
		
		var $moodName;
		var $picture;
		var $music;
		var $isActivated;
		var $position;
		
		public function Mood($data=0){
			if(is_array($data)) $this->create($data);
			elseif(is_string($data)) $this->load($data);
		}
		
		public function load($id){
			$db = new HumeurDatabase();
			$data = mysql_fetch_assoc($db->query("SELECT * FROM moods WHERE moodName = '".$id."' LIMIT 1;"));
			$this->create($data);
		}
		
		public function create($array){
			if(isset($array['moodName'])) $this->moodName = $array['moodName'];
			if(isset($array['picture'])) $this->picture = $array['picture'];
			if(isset($array['music'])) $this->music = $array['music'];
			if(isset($array['isActivated'])) $this->isActivated = $array['isActivated'];
			if(isset($array['position'])) $this->position = $array['position'];
		}
		
		public function save($pictureFile="",$musicFile=""){
			$db = new HumeurDatabase();
			if(mysql_num_rows($db->query("SELECT * FROM moods WHERE moodName = '".$this->moodName."' LIMIT 1;")) > 0) return $this->update($pictureFile,$musicFile);
			else return $this->insert($pictureFile,$musicFile);
		}
		
		public function getNextPosition(){
			$db = new HumeurDatabase();
			return mysql_num_rows($db->query("SELECT * FROM moods")) + 1;
		}
		
		public function insert($pictureFile,$musicFile){
			$id = uniqid();
			$uploader = new Uploader($pictureFile,$_SERVER['DOCUMENT_ROOT']."/img/moods/");
			$this->picture = $uploader->upload($id);
			
			$uploader = new Uploader($musicFile,$_SERVER['DOCUMENT_ROOT']."/music/moods/");
			$this->music = $uploader->upload($id);
			$db = new HumeurDatabase();
			return $db->query("INSERT INTO moods VALUES ('".$this->moodName."','".$this->picture."','".$this->music."','1','".$this->getNextPosition()."');");
		}
		
		public function delete(){
			$db = new HumeurDatabase();
			return $db->query("DELETE FROM moods WHERE moodName = '".$this->moodName."';");
		}
		
		public function update($pictureFile="",$musicFile=""){
			$id = uniqid();
			if(!empty($pictureFile['name'])){
				$uploader = new Uploader($pictureFile,$_SERVER['DOCUMENT_ROOT']."/img/moods/");
				$this->picture = $uploader->upload($id);
			}
			
			if(!empty($musicFile['name'])){
				$uploader = new Uploader($musicFile,$_SERVER['DOCUMENT_ROOT']."/music/moods/");
				$this->music = $uploader->upload($id);
			}
			$db = new HumeurDatabase();
			return $db->query("UPDATE moods SET picture = '".$this->picture."', music = '".$this->music."',isActivated = '".$this->isActivated."', position = '".$this->position."' WHERE moodName = '".$this->moodName."';");
		}
		
		public function toggleActivation(){
			$this->isActivated = ($this->isActivated == 1)? 0:1;
		}
		
		public function setPosition($position){
			$db = new HumeurDatabase();
			$this->position = 0;
			$this->save();
			$data = $db->query("SELECT * FROM moods WHERE position > 0 ORDER BY position ASC;");
			$pos = 1;
			while($mood = mysql_fetch_assoc($data)){
				if($pos == $position) $pos++;
				$db->query("UPDATE moods SET position = '".$pos."' WHERE moodName = '".$mood['moodName']."';");
				$pos++;
			}
			$this->position = $position;
			$this->save();
		}

		public function summary(){
			return '<div id="'.$this->moodName.'" class="'.(($this->isActivated == 1)? 'activated':'disactivated').' existingColorBloc centered">
                        '.$this->moodName.'<br/>
						Position : '.$this->position.'<br/>
                        <img src="img/moods/'.$this->picture.'" />
                        <div>
                            <object type="application/x-shockwave-flash" data="music/dewplayer.swf" width="200" height="20" id="dewplayer" name="dewplayer">
                                <param name="movie" value="music/dewplayer.swf" />
                                <param name="flashvars" value="mp3=music/moods/'.$this->music.'" />
                                <param name="wmode" value="transparent" />
                            </object>
                       </div>
                        <a onclick="ajaxLoad(\'makeMoodEditForm\',\'id='.$this->moodName.'\',makeMoodEditForm_response)"><img src="img/editMember.png" alt="Editer"/></a>
                        <a onclick="ajaxLoad(\'deleteMood\',\'id='.$this->moodName.'\',deleteMood_response)">'.(($this->isActivated == 1)? '<img src="img/activeColor.png" alt="Activer"/>':'<img src="img/deleteMember.png" alt="Désactiver"/>').'</a>
                    </div>';
		}
		
		public function editForm(){
			return '<div id="'.$this->moodName.'" class="'.(($this->isActivated == 1)? 'activated':'disactivated').' existingColorBloc centered">
						<form action="" method="post" enctype="multipart/form-data">
						<input type="hidden" name="MAX_FILE_SIZE" value="300000000">
						'.$this->moodName.'<br/>
						Image : <br/>
						<input type="file" name="picture'.$this->moodName.'" placeholder="Image" /><br/>
						Musique d\'ambiance : <br/>
						<input type="file" name="music'.$this->moodName.'" placeholder="Musique" /><br/>
						Position : <br/><input type="text" name="'.$this->moodName.'position" value="'.$this->position.'"/><br/>
						<input type="submit" name="submit'.$this->moodName.'">
					</form>
				</div>';
		}
		
		public function __toString(){
			return $this->colorName;
		}
		
	}
?>